package com.cmcc.aregister;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.http.HttpAsyncTask;
import com.cmcc.aregister.cs.util.MyLogger;
import com.cmcc.aregister.ui.activity.ParentActivity;

/**
 * 
 * @author swq
 * 
 */
public class Controller {
	private final static String TAG = "Controller";
	private static final MyLogger logger = MyLogger.getLogger();
	private static final int CORE_POOL_SIZE = 5;
	private static final int MAXIMUM_POOL_SIZE = 15;
	private static final int KEEP_ALIVE = 5;
	private ParentActivity activity = null;
	private static final BlockingQueue<Runnable> sWorkQueue = new LinkedBlockingQueue<Runnable>(
			MAXIMUM_POOL_SIZE);

	private static final ThreadFactory sThreadFactory = new ThreadFactory() {
		private AtomicInteger mCount = new AtomicInteger(1);

		public Thread newThread(Runnable r) {
			int count = mCount.getAndIncrement();
			return new Thread(r, count + "_thread");
		}
	};
	private static RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();

	private static Controller _controller = null;
	private Handler _controlHandler = null;

	private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor(
			CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
			sWorkQueue, sThreadFactory, rejectedExecutionHandler);

	private Controller() {

	}

	public static Controller getInstance() {
		if (_controller == null) {
			_controller = new Controller();
		}
		return _controller;
	}

	public void setCxt(Context cxt) {
		if (cxt instanceof ParentActivity)
			this.activity = (ParentActivity) cxt;
	}

	/**
	 * 执行一个 RUN 任务
	 * 
	 * @param httpThreadTask
	 */
	public synchronized void execute(HttpAsyncTask httpThreadTask) {
		if (httpThreadTask != null) {
			if (httpThreadTask._cacheType == HttpAsyncTask.CACHE_TYPE_SAVE) {
				boolean flag = httpThreadTask.onPreExecuteCheckCache();
				if (flag) {
					httpThreadTask.isNotifyUI = false;
				} else {
					showProDialog(httpThreadTask);
				}
			}
			if (httpThreadTask.isNeedUpadteCache) {
				if(ConfigUtilHelper.debug){
					Log.i(TAG,"sExecutor.execute:" + httpThreadTask.get_jsonParseType() + httpThreadTask.get_cacheType());
				}
				sExecutor.execute(httpThreadTask);
			}
		}
	}

	private void showProDialog(HttpAsyncTask httpThreadTask) {
		try {
			if (activity != null && !httpThreadTask.isShow) {
				activity.showProgressDialog();
			} else {
				logger.e(">>>>activity is null>>>>>>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Handler getControlHandler() {
		if (_controlHandler != null) {
			return _controlHandler;
		}
		return null;
	}

	/**
	 * 清空请求队列
	 */
	public void release() {
		if (sExecutor != null) {
			sExecutor.shutdownNow();
		}
		_controlHandler = null;
	}

}
